package com.atguigu.juc.day02;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
 * @author atguigu-mqx
 */
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {

        //  先声明一个阻塞队列 参数：队列中要存储的数据个数！
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<String>(3);



        //  异常---------------
//        System.out.println(arrayBlockingQueue.add("a"));
//        System.out.println(arrayBlockingQueue.add("b"));
//        System.out.println(arrayBlockingQueue.add("c"));
//        System.out.println(arrayBlockingQueue.remove("a"));
//        System.out.println(arrayBlockingQueue.remove());
//        System.out.println(arrayBlockingQueue.add("d"));
//        System.out.println(arrayBlockingQueue.element());
//        System.out.println(arrayBlockingQueue.element());
//        System.out.println(arrayBlockingQueue.element());

        //  特殊值-----
//        System.out.println(arrayBlockingQueue.offer("a"));
//        System.out.println(arrayBlockingQueue.offer("b"));
//        System.out.println(arrayBlockingQueue.offer("c"));
        //  没有添加到队列。
//        System.out.println(arrayBlockingQueue.offer("d"));

//        System.out.println(arrayBlockingQueue.poll());
        //  获取到的是谁? b
//        System.out.println(arrayBlockingQueue.peek());
//        System.out.println(arrayBlockingQueue.poll());
//        System.out.println(arrayBlockingQueue.poll());
//        System.out.println(arrayBlockingQueue.poll());

//          阻塞-----------------
//        arrayBlockingQueue.put("a");
//        arrayBlockingQueue.put("b");
//        arrayBlockingQueue.put("c");
        //  当阻塞队列满的时候，再放入数据，则会发生阻塞!
        // arrayBlockingQueue.put("d");

        //  当阻塞队列为空的时候，再获取数据的时候，则会发生阻塞！
        System.out.println(arrayBlockingQueue.take());

//      超时：
//        System.out.println(arrayBlockingQueue.offer("a"));
//        System.out.println(arrayBlockingQueue.offer("b"));
//        System.out.println(arrayBlockingQueue.offer("c"));
//        System.out.println(arrayBlockingQueue.offer("d", 3, TimeUnit.SECONDS));

//        System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS));
    }
}
